$(function () {

    jqueryI18next.init(i18next, $);
    var data = {
        "student": [{
                "name": "张三",
                "age": 18,
                "sex": "男",
                "title": "title",
            },
            {
                "name": '张四',
                "sex": "男",
                "age": 22
            },
            {
                "name": "李四",
                "sex": "男",
                "age": 18,
                "title": 'title',
            }
        ]

    };
    var myTemplate = Handlebars.compile($("#table-template").html());
    //将json对象用刚刚注册的Handlebars模版封装，得到最终的html，插入到基础table中。
    $('#tableList').html(myTemplate(data));
    var defaults = localStorage.getItem('i18nextLng') || localStorage.setItem('i18nextLng', 'zh-CN');

    i18next.init({
        lng: defaults,
        fallbackLng: 'zh-CN', //初始化语言
        debug: true,

        // useLocalStorage: true,
        // caches: ['localStorage', 'cookie'],
        // lookupQuerystring: 'lng',
        // lookupCookie: 'i18next',
        // lookupLocalStorage: 'i18nextLng',

        resources: {
            'zh-CN': {
                translation: {
                    "title": "中国",
                    "table": {
                        "name": "姓名",
                        "sex": "性别",
                        "age": "年龄"
                    }
                }
            },
            'en': {
                translation: {
                    "title": "中国",
                    "table": {
                        "name": "name",
                        "sex": "sex",
                        "age": "age"
                    }
                }
            }
        },

    }, function (res, t) {


        $('[data-i18n]').localize();

    })

    defaults = localStorage.getItem('i18nextLng') || 'zh-CN'; //获取当前cookie中设置的语言

    var selected = $("#language");
    selected.find('option[value=' + defaults + ']').prop('selected', true);

    selected.on('change', function (event) {
        var $this = $(event.currentTarget)
        var lng = $this.find('option:selected').val();

        i18next.changeLanguage(lng, function (res, t) {
            localStorage.setItem('i18nextLng', lng);
            $('[data-i18n]').localize();
        });
    });
});
